CREATE PROCEDURE coreCmsPublishVersion
(
	@cid			int,
	@OwningUserID	int,
	@VersionCount	int
)
AS
BEGIN
	SET NOCOUNT ON;

	/* Declarations */
	DECLARE		@versionID	int;
	DECLARE		@versionNumber int;
	
	/* Get the version record id */
	SELECT @versionID = ISNULL(FK_VersionV, -1) FROM Content WHERE PK_ID=@cid;
	
	/* UPDATE THE RECORDS */
	UPDATE Content SET
		WorkflowStatus			= 3,
		LastModifiedDT			= GETDATE(),
		FK_LastModifiedBy		= @OwningUserID,
		FK_LiveV				= @versionID
	WHERE PK_ID=@cid;
	
	/* CLEAR ANY OLD VERSIONS */
	IF @VersionCount > 0
	BEGIN
		SELECT @versionNumber = VersionNumber FROM ContentV WHERE PK_ID=@versionid;
		DELETE FROM ContentV WHERE FK_ContentID=@cid AND VersionNumber < (@versionNumber-@VersionCount);
	END

	SET NOCOUNT OFF;	
END